//
//  NSString+Password.m
//  DaCheTong
//
//  Created by HR on 16/9/20.
//  Copyright © 2016年 dawenkeji. All rights reserved.
//

#import "NSString+Password.h"
#import <CommonCrypto/CommonDigest.h>

static NSString *token = @"fashfkdashfjkldashfjkdashfjkdahsfjdasjkvcxnm%^&%^$&^uireqwyi1237281643";
static NSMutableString *string = nil;
@implementation NSString (Password)

-(NSString *)myMD5
{
    NSString *String = [NSString stringWithFormat:@"%@%@",self,token];
    //调用md5方法
    [self MD5];
    //返回二次加密
    return [String MD5data];
}
#pragma mark 使用32位MD5加密字符串
- (NSString *)MD5
{
    const char *cStr = [self UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];

    CC_MD5(cStr, strlen(cStr), digest);

    NSMutableString *result = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
    {
        [result appendFormat:@"%02x", digest[i]];

    }
    //按照index(20,28)切割字符串
    NSString *substr1 = [result substringWithRange:NSMakeRange(10, 18)];

    //按照index(10,18)切割字符串
    NSString *substr2 = [result substringWithRange:NSMakeRange(20, 12)];

    //拼接截取出来的字符串
    string = [NSMutableString stringWithFormat:@"%@%@",substr1,substr2];
    return string;

}
//将截取并拼接好的字符串传出，进行md5加密
- (NSString *)MD5data
{

    const char *Str = [string UTF8String];
    unsigned char digests[CC_MD5_DIGEST_LENGTH];

    CC_MD5(Str, strlen(Str), digests);

    NSMutableString *str = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];

    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++)
    {
        [str appendFormat:@"%02x", digests[i]];

    }

    return str;
}


@end
